package com.shtb.wisdom.videotaker

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(findViewById(R.id.toolbar))

        findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
            try {
                val perms = arrayOf<String>(android.Manifest.permission.CAMERA, android.Manifest.permission.RECORD_AUDIO)
                if (EasyPermissions.hasPermissions(this, *perms)) {
                    val intent = Intent(this,VideoTakerActivity::class.java)
                    intent.putExtra("recordMaxTime", 300)
                    startActivityForResult(intent, 99)
                } else {
                    EasyPermissions.requestPermissions(this, "拍视频需要您同意摄像头和录音权限", AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE, *perms)
                }

            } catch (e: Exception) {
                runOnUiThread {
                    Toast.makeText(this, "打开摄像头失败！", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}